package handler

import (
	__ "api/proto"
	"api/request"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"net/http"
)

func AddOrder(c *gin.Context) {
	var req request.AddOrder
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "验证失败",
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewEatClient(conn)
	order, err := c1.AddOrder(c, &__.AddOrderRequest{
		Userid:  int64(req.UserId),
		Ordersn: req.OrderSn,
		PayType: int64(req.PayType),
		Status:  int64(req.Status),
		Amount:  float32(req.Amount),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "下单失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"data": order,
		"msg":  "下单成功",
	})
	return
}

func ListOrder(c *gin.Context) {
	var req request.ListOrder
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "验证失败",
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewEatClient(conn)
	xx, err := c1.OrderList(c, &__.OrderListRequest{
		Page: int64(req.Page),
		Size: int64(req.Size),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "展示失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"data": xx,
		"msg":  "展示成功",
	})
	return
}

func OrderInfo(c *gin.Context) {
	var req request.OrderInfo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "验证失败",
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewEatClient(conn)
	xx, err := c1.OrderInfo(c, &__.OrderInfoRequest{
		Id: int64(req.Id),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "展示失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"data": xx,
		"msg":  "展示成功",
	})
	return
}

func Del(c *gin.Context) {
	var req request.OrderDel
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "验证失败",
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewEatClient(conn)
	xx, err := c1.OrderDel(c, &__.OrderDelRequest{
		Id: int64(req.Id),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"data": err.Error(),
			"msg":  "删除失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"data": xx,
		"msg":  "订单超时 已取消",
	})
	return
}
